package com.biz.primus.ms.adv.trans;

import com.biz.primus.model.adv.vo.AdvertisementVo;
import com.biz.primus.ms.adv.dao.po.Advertisement;
import com.google.common.base.Function;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author jun.liu
 * @date 2017/9/18
 */
@Slf4j
public class AdvertisementVo2Advertisement implements Function<AdvertisementVo, Advertisement> {

    @Override
    public Advertisement apply(AdvertisementVo input) {
        Advertisement po = new Advertisement();
        if (input.getId() != null && !"".equals(input.getId().trim())) {
            po.setId(Long.parseLong(input.getId()));
        }
        po.setTitle(input.getTitle());
        po.setUrl(input.getUrl());
        po.setAtype(input.getAtype());
        po.setSourcePath(input.getSourcePath());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Instant beginInstant = sdf.parse(input.getBeginDate()).toInstant();
            ZoneId beginZone = ZoneId.systemDefault();
            LocalDateTime beginLocalDateTime = LocalDateTime.ofInstant(beginInstant, beginZone);

            Instant endInstant = sdf.parse(input.getEndDate()).toInstant();
            ZoneId endZone = ZoneId.systemDefault();
            LocalDateTime endLocalDateTime = LocalDateTime.ofInstant(endInstant, endZone);

            po.setBeginDate(beginLocalDateTime);
            po.setEndDate(endLocalDateTime);
        } catch (Exception e) {
            log.error("时间转化异常---》" + e.getMessage());
        }
        po.setSupportedGeoId(input.getSupportedGeoId());
        po.setCountryStatus(input.isCountryStatus());
        po.setSecond(input.getSecond());
        return po;
    }
}
